﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.Legency;
using ZenSystemService.Api.DTOs.Users;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    [Route("api/users")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly UserService _userService;
        private readonly SMSVerificationCodeService _smsVerificationCodeService;
        private readonly DTOBusiness _dTOBusiness;
        private readonly TokenService _tokenService;
        public UserController(UserService userService, SMSVerificationCodeService smsVerificationCodeService
            , DTOBusiness dTOBusiness, TokenService tokenService,
            IConfiguration configuration)
        {
            _userService = userService;
            _smsVerificationCodeService = smsVerificationCodeService;
            _dTOBusiness = dTOBusiness;
            _tokenService = tokenService;
        }

        /// <summary>
        /// 得到所有教练信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetAll")]
        //[Authorize(Roles = "Admin")]
        public async Task<ReturnObjectT<IEnumerable<User>>> GetAll()
        {
            ReturnObjectT<IEnumerable<User>> _returnObject = new ReturnObjectT<IEnumerable<User>>();
            var coachs = await _userService.GetAllUsersAsync();

            _returnObject.Code = 1;
            _returnObject.Data = coachs;

            return _returnObject;
        }

        /// <summary>
        /// 根据ID 得到User实体信息
        /// </summary>
        /// <param name="_receiveObjectT"></param>
        /// <returns></returns>
        [HttpPost("GetUserById")]
        public async Task<ReturnObjectT<User>> GetUserById(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObjectT<User> _returnObject = new ReturnObjectT<User>();
 
            int id = _receiveObjectT.Data;
            var coach = await _userService.GetUserByIdAsync(id);
            if (coach == null)
            {
                //  return NotFound();
                _returnObject.ErrorMsg = "未找到数据";
                _returnObject.Code = 0;
                return _returnObject;
            }
            else
            {
                _returnObject.Code = 1;
                _returnObject.Data = coach;
            }
            return _returnObject;
        }

        /// <summary>
        /// 根据ID 得到User实体信息
        /// </summary>
        /// <param name="_receiveObjectT"></param>
        /// <returns></returns>
        [HttpPost("GetUsersByRole")]
        public ReturnObjectT<IEnumerable<UserDTO>> GetUsersByRole(ReceiveObjectT<GetUserByRoleCriteriaDTO> _receiveObjectT)
        {
            ReturnObjectT<IEnumerable<UserDTO>> _returnObject = new ReturnObjectT<IEnumerable<UserDTO>>();

            var criteria = _receiveObjectT.Data;
            var users = _userService.GetUsersByRoleAsync(criteria);
            if (users == null)
            {
                //  return NotFound();
                _returnObject.ErrorMsg = "未找到数据";
                _returnObject.Code = 0;
                return _returnObject;
            }
            else
            {
                _returnObject.Code = 1;
                _returnObject.Data = users;
            }
            return _returnObject;
        }

        /// <summary>
        /// 创建User信息
        /// </summary>
        /// <param name="coach"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<IActionResult> CreateUser(UserCreateDTO user)
        {
            var currentUser = User.ToUserModel();
            var newUser = await _userService.CreateUserAsync(currentUser, user);
            if (newUser != null)
            {
                return CreatedAtAction(nameof(GetUserById),
                new { id = newUser.UserId }, newUser);
            }
            else
            {
                return BadRequest("Create user failed");
            }
           // return CreatedAtAction(nameof(CreateCoach), new { id = coach.Id }, coach);
        }

        /// <summary>
        /// 保存评估软件过来的学员信息(临时使用)
        /// 机构Id从配置文件获取
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("CreateForLegencyUser")]
        public async Task<IActionResult> CreateForLegencyUser(LegencyUserCreateDTO user)
        {
            var newUser = await _userService.CreateForLegencyUserAsync(user);
            if (newUser != null)
            {
                return CreatedAtAction(nameof(GetUserById),
                new { id = newUser.UserId }, newUser);
            }
            else
            {
                return BadRequest("Create user failed");
            }
        }

        /// <summary>
        /// 更新人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="coach"></param>
        /// <returns></returns>
        [HttpPost("Update")]
        public async Task<IActionResult> Update(UserUpdateRequestDTO user)
        {
            var newUser = await _userService.UpdateUserAsync(user);

            if (newUser != null)
            {
                return Ok(newUser);
            }
            else
            {
                return BadRequest("update user failed");
            }
        }


        /// <summary>
        /// 删除User信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("DeleteUser")]
        public async Task<ReturnObject> Delete(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObject _returnObject = new ReturnObject();
                        
            int id = _receiveObjectT.Data;
            bool isSuccess =  await _userService.DeleteUserAsync(id);
            if (isSuccess)
            {
                _returnObject.Code = 1;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "删除教练信息失败";
            }
            return _returnObject;
            //  return NoContent();
        }


     


    }
}